package com.funsport.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.entity.Role;
import com.funsport.entity.RolePermission;
import com.funsport.mapper.RoleMapper;
import com.funsport.mapper.RolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色服务
 */
@Service
public class RoleService {
    
    @Autowired
    private RoleMapper roleMapper;
    
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    
    /**
     * 分页查询角色列表
     */
    public Page<Role> getPage(int page, int pageSize, String keyword) {
        Page<Role> rolePage = new Page<>(page, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.and(w -> w.like("name", keyword).or().like("code", keyword));
        }
        
        wrapper.orderByAsc("sort_order");
        
        return roleMapper.selectPage(rolePage, wrapper);
    }
    
    /**
     * 获取所有角色
     */
    public List<Role> getAllRoles() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "active");
        wrapper.orderByAsc("sort_order");
        return roleMapper.selectList(wrapper);
    }
    
    /**
     * 根据ID获取角色
     */
    public Role getById(Long id) {
        return roleMapper.selectById(id);
    }
    
    /**
     * 根据编码获取角色
     */
    public Role getByCode(String code) {
        return roleMapper.selectByCode(code);
    }
    
    /**
     * 创建角色
     */
    public Role create(Role role) {
        roleMapper.insert(role);
        return role;
    }
    
    /**
     * 更新角色
     */
    public Role update(Role role) {
        roleMapper.updateById(role);
        return role;
    }
    
    /**
     * 删除角色
     */
    @Transactional
    public void delete(Long id) {
        // 删除角色的所有权限关联
        rolePermissionMapper.deleteByRoleId(id);
        // 删除角色
        roleMapper.deleteById(id);
    }
    
    /**
     * 获取角色的权限ID列表
     */
    public List<Long> getRolePermissionIds(Long roleId) {
        return rolePermissionMapper.selectPermissionIdsByRoleId(roleId);
    }
    
    /**
     * 分配权限给角色
     */
    @Transactional
    public void assignPermissions(Long roleId, List<Long> permissionIds) {
        // 先删除该角色的所有权限
        rolePermissionMapper.deleteByRoleId(roleId);
        
        // 重新分配权限
        if (permissionIds != null && !permissionIds.isEmpty()) {
            for (Long permissionId : permissionIds) {
                RolePermission rp = new RolePermission();
                rp.setRoleId(roleId);
                rp.setPermissionId(permissionId);
                rolePermissionMapper.insert(rp);
            }
        }
    }
    
    /**
     * 根据用户ID获取角色列表
     */
    public List<Role> getRolesByUserId(Long userId) {
        return roleMapper.selectByUserId(userId);
    }
}

